package com.igoal.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.comparator.GoalEntryComparator;
import com.igoal.dto.GoalDTO;
import com.igoal.dto.UserDTO;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;
import com.igoal.service.paging.PageItem;
import com.igoal.service.paging.PagingService;
import com.igoal.utils.WebUtils;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.UserPublicProfileModel;

@Controller
@RequestMapping("/users.html")
public class UserPublicProfileController extends BaseController {
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}
	
	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager){
		this.userGoalManager = userGoalManager;
	}
	
	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}
	
	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}
	
	private UserDTO publicUser = null;
	private UserDTO sessionUser = null;

	@RequestMapping(method = RequestMethod.GET)
	public String showPublicProfile(ModelMap model, HttpServletRequest request,
			@RequestParam("username") String username) {
		publicUser = userManagerExt.getUserByUsername(username);

		if (isLoginUser()) {
			sessionUser = getUserSession();
			if (publicUser != null && sessionUser.getUsername().equals(publicUser.getUsername())) {
				return "redirect:/dashboard.html";
			}
		}

		if (publicUser == null) {
			return "redirect:/error/505.html?page=http://igoal.vn/" + username;
		}

		initModel(model, sessionUser, publicUser, request);
		return getViewPrefix() + "userPublicProfile";
	}

	private void initModel(ModelMap modelMap, UserDTO user, UserDTO publicUser, HttpServletRequest request) {
		if (modelMap == null || publicUser == null)
			return;

		UserPublicProfileModel model = new UserPublicProfileModel();
//		initUserAvatar(publicUser, model);
//		initUserStatus(publicUser, model);
//		initRightZone(publicUser, model);
//		initSessionInformation(model);
//
//		if (user != null && !user.getUsername().equals(publicUser.getUsername())) {
//			model.setFollowed(userManagerExt.isFriend(user, publicUser));
//			model.setPendingAcceptFollow(userManagerExt.isWaitingAcceptFriend(user, publicUser));
//			initMutualFriends(userManagerExt, user, publicUser, model);
//			initPeopleYouMayKnow(user, model);
//
//			if (model.getUserStatus() != null && model.getUserStatus().getUserStatusId() != null) {
//				Boolean thumbStatus = userManagerExt.getUserThumbedStatus(user.getUsername(), model
//						.getUserStatus().getUserStatusId());
//				if (thumbStatus != null) {
//					model.getUserStatus().setThumbStatus(thumbStatus);
//				}
//			}
//		} else {
//			model.setFollowed(false);
//		}
//
//		model.setUsername(publicUser.getUsername());
//		model.setUserId(publicUser.getId());
//		model.setDoingGoals(userGoalManager.getDoingGoal(publicUser.getId()));
//		model.setCompletedGoals(userGoalManager.getCompletedGoal(publicUser.getId()));
//
//		initInformationBox(publicUser, model);
//
//		UserDTO loginUser = null;
//		if (isLoginUser()) {
//			loginUser = getUserSession();
//		}
//		List<PageItem<Long>> recentEntries = goalEntryManager.getRecentEntries(publicUser, loginUser);
//		Collections.sort(recentEntries, new GoalEntryComparator(false));
//
//		List<PageItem<Long>> result = new ArrayList<PageItem<Long>>();
//
//		for (int index = 0; index < PagingService.MAX_ENTRIES && index < recentEntries.size(); index++) {
//			result.add(recentEntries.get(index));
//		}
//
//		model.setRecentEntries(result);
//
//		List<GoalDTO> topGoals = goalManager.getTopGoals(user);
//		model.setTopGoals(WebUtils.buildGoalCloud(topGoals));
//
//		List<GoalDTO> commonGoals = goalManager.getCommonGoals(user);
//		model.setCommonGoals(WebUtils.buildGoalCloud(commonGoals));

		initTopPage(model, request);
		initRightPage(model, request);
		initLeftPage(model, request);		
		applyModel(modelMap, model);
	}

	public void initTopPage(BaseModel baseModel, HttpServletRequest request) {
		UserPublicProfileModel model = (UserPublicProfileModel) baseModel;
		
		initUserAvatar(publicUser, model);
		initUserStatus(publicUser, model);
		initSessionInformation(model);
		model.setUsername(publicUser.getUsername());
		model.setFirstName(publicUser.getFirstName());
		model.setUserId(publicUser.getId());
		
		if (sessionUser != null && !sessionUser.getUsername().equals(publicUser.getUsername())) {
			model.setFollowed(userManagerExt.isFriend(sessionUser, publicUser));
			model.setPendingAcceptFollow(userManagerExt.isWaitingAcceptFriend(sessionUser, publicUser));			

			if (model.getUserStatus() != null && model.getUserStatus().getUserStatusId() != null) {
				Boolean thumbStatus = userManagerExt.getUserThumbedStatus(sessionUser.getUsername(), model.getUserStatus().getUserStatusId());
				if (thumbStatus != null) {
					model.getUserStatus().setThumbStatus(thumbStatus);
				}
			}
		} else {
			model.setFollowed(false);
		}
	}

	public void initRightPage(BaseModel baseModel, HttpServletRequest request) {
		UserPublicProfileModel model = (UserPublicProfileModel) baseModel;
		if(sessionUser != null && !sessionUser.getUsername().equals(publicUser.getUsername())){
			initMutualFriends(userManagerExt, sessionUser, publicUser, model);
			initPeopleYouMayKnow(sessionUser, model);
		}
		
		initInformationBox(publicUser, model);
	}

	@SuppressWarnings("unchecked")
	public void initLeftPage(BaseModel baseModel, HttpServletRequest request) {
		UserPublicProfileModel model = (UserPublicProfileModel) baseModel;
		model.setDoingGoals(userGoalManager.getDoingGoal(publicUser.getId()));
		model.setCompletedGoals(userGoalManager.getCompletedGoal(publicUser.getId()));
		
		List<PageItem<Long>> recentEntries = goalEntryManager.getRecentEntries(publicUser, sessionUser);
		Collections.sort(recentEntries, new GoalEntryComparator(false));

		List<PageItem<Long>> result = new ArrayList<PageItem<Long>>();

		for (int index = 0; index < PagingService.MAX_ENTRIES && index < recentEntries.size(); index++) {
			result.add(recentEntries.get(index));
		}

		model.setRecentEntries(result);

		List<GoalDTO> topGoals = goalManager.getTopGoals(sessionUser);
		model.setTopGoals(WebUtils.buildGoalCloud(topGoals));

		List<GoalDTO> commonGoals = goalManager.getCommonGoals(sessionUser);
		model.setCommonGoals(WebUtils.buildGoalCloud(commonGoals));
	}
}
